<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="状态
state =0 未同步
state =1 同步" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态
state =0 未同步
state =1 同步"></el-input>
    </el-form-item>
    <el-form-item label="content内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="content内容"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="覆盖人群" prop="coverpsn">
      <el-input v-model="dataForm.coverpsn" placeholder="覆盖人群"></el-input>
    </el-form-item>
    <el-form-item label="新建时间" prop="creationtime">
      <el-input v-model="dataForm.creationtime" placeholder="新建时间"></el-input>
    </el-form-item>
    <el-form-item label="失效时间" prop="invalidtime">
      <el-input v-model="dataForm.invalidtime" placeholder="失效时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifytime">
      <el-input v-model="dataForm.modifytime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="dr逻辑删除标记" prop="dr">
      <el-input v-model="dataForm.dr" placeholder="dr逻辑删除标记"></el-input>
    </el-form-item>
    <el-form-item label="时间戳" prop="ts">
      <el-input v-model="dataForm.ts" placeholder="时间戳"></el-input>
    </el-form-item>
    <el-form-item label="预留01" prop="def01">
      <el-input v-model="dataForm.def01" placeholder="预留01"></el-input>
    </el-form-item>
    <el-form-item label="预留02" prop="def02">
      <el-input v-model="dataForm.def02" placeholder="预留02"></el-input>
    </el-form-item>
    <el-form-item label="预留03" prop="def03">
      <el-input v-model="dataForm.def03" placeholder="预留03"></el-input>
    </el-form-item>
    <el-form-item label="预留04" prop="def04">
      <el-input v-model="dataForm.def04" placeholder="预留04"></el-input>
    </el-form-item>
    <el-form-item label="预留05" prop="def05">
      <el-input v-model="dataForm.def05" placeholder="预留05"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          state: '',
          content: '',
          remarks: '',
          coverpsn: '',
          creationtime: '',
          invalidtime: '',
          modifytime: '',
          dr: '',
          ts: '',
          def01: '',
          def02: '',
          def03: '',
          def04: '',
          def05: ''
        },
        dataRule: {
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态
state =0 未同步
state =1 同步不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: 'content内容不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          coverpsn: [
            { required: true, message: '覆盖人群不能为空', trigger: 'blur' }
          ],
          creationtime: [
            { required: true, message: '新建时间不能为空', trigger: 'blur' }
          ],
          invalidtime: [
            { required: true, message: '失效时间不能为空', trigger: 'blur' }
          ],
          modifytime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          dr: [
            { required: true, message: 'dr逻辑删除标记不能为空', trigger: 'blur' }
          ],
          ts: [
            { required: true, message: '时间戳不能为空', trigger: 'blur' }
          ],
          def01: [
            { required: true, message: '预留01不能为空', trigger: 'blur' }
          ],
          def02: [
            { required: true, message: '预留02不能为空', trigger: 'blur' }
          ],
          def03: [
            { required: true, message: '预留03不能为空', trigger: 'blur' }
          ],
          def04: [
            { required: true, message: '预留04不能为空', trigger: 'blur' }
          ],
          def05: [
            { required: true, message: '预留05不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dmpf/psndatas/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.psndatas.code
                this.dataForm.name = data.psndatas.name
                this.dataForm.state = data.psndatas.state
                this.dataForm.content = data.psndatas.content
                this.dataForm.remarks = data.psndatas.remarks
                this.dataForm.coverpsn = data.psndatas.coverpsn
                this.dataForm.creationtime = data.psndatas.creationtime
                this.dataForm.invalidtime = data.psndatas.invalidtime
                this.dataForm.modifytime = data.psndatas.modifytime
                this.dataForm.dr = data.psndatas.dr
                this.dataForm.ts = data.psndatas.ts
                this.dataForm.def01 = data.psndatas.def01
                this.dataForm.def02 = data.psndatas.def02
                this.dataForm.def03 = data.psndatas.def03
                this.dataForm.def04 = data.psndatas.def04
                this.dataForm.def05 = data.psndatas.def05
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/dmpf/psndatas/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'state': this.dataForm.state,
                'content': this.dataForm.content,
                'remarks': this.dataForm.remarks,
                'coverpsn': this.dataForm.coverpsn,
                'creationtime': this.dataForm.creationtime,
                'invalidtime': this.dataForm.invalidtime,
                'modifytime': this.dataForm.modifytime,
                'dr': this.dataForm.dr,
                'ts': this.dataForm.ts,
                'def01': this.dataForm.def01,
                'def02': this.dataForm.def02,
                'def03': this.dataForm.def03,
                'def04': this.dataForm.def04,
                'def05': this.dataForm.def05
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
